<?php


namespace App\Http\Repositories\ProductCenter;

use App\Exceptions\ApiException;
use App\Http\Repositories\BaseRepository;
use App\Models\ProductCenter\Models\SkuInfoModel;
use App\Models\ProductCenter\Models\SkuModel;
use App\Models\ProductCenter\Models\SpuModel;
use Illuminate\Support\Facades\Redis;

class SpuRepository extends BaseRepository
{
    public $spu;
    public function __construct(SpuModel $spu)
    {
        $this->spu = $spu;
    }

    /**
     * 添加
     * @param $data
     * @return array
     */
    public function addSpu($data)
    {
        $data['spu'] = getSpuCode();

        $spu = $this->spu->addItem($data, true);

        isset($data['sku']) && $this->createSku($spu, $data['sku']);

        return [];
    }

    public function createSku(SpuModel $spu, $sku, $isAdd = true)
    {
        if (!empty($sku)) {
            if (!$isAdd) {
                $spu->sku()->get()->map(function ($sku, $key) {
                    $sku->delete();
                });
            }

            foreach ($sku as $v) {
                $v = $this->spu->setUserInfo($v);
                $v['spu'] = $spu->spu;
                $skuModel = $spu->sku()->create($v);
                if (isset($v['sku_info'])) {
                    foreach ($v['sku_info'] as $j) {
                        $j['size_id'] = $j['id'];
                        $skuModel->skuInfo()->create($j);
                    }
                }
            }
        }
    }

    public function getSpuList($data)
    {
        for($i = 0; $i<10; $i++) {
            $a['id'] = $i+1;
            $a['count'] = rand(1,100);
            Redis::zAdd('a', json_encode($a), $i*10);
        }
        dd(Redis::zRange('a', 0, -1));

        return $this->spu->getSpuList($data);
    }

    /**
     * 删除
     * @param $data
     * @return string[]
     */
    public function delSpu($data)
    {
        $ids = explode(',', $data['ids']);

        foreach ($ids as $v) {
            $spu = $this->spu->handleCondition(['id' => $v])->first();
            if (is_null($spu))
                $spu->delete();
        }

        return [];
    }

    /**
     * 获取详情
     * @param $data
     * @return array|mixed
     */
    public function getSpuInfo($data)
    {
        is_real_exists(@$data['id']) && $where['id'] = $data['id'];
        is_real_exists(@$data['spu']) && $where['spu'] = $data['spu'];

        return $this->spu->getSpuInfo($where ?? []);
    }

    /**
     * 编辑状态
     * @param $data
     * @return array
     */
    public function editSpuStatus($data)
    {
        $spu = $this->spu->find($data['id']);
        if (is_null($spu))
            $this->throwError();

        $update['status'] = $data['status'];
        $spu->update($update);

        return [];
    }

    /**
     * 编辑
     * @param $data
     * @return array
     * @throws ApiException
     */
    public function editSpu($data)
    {
        $spu = $this->spu->find($data['id']);
        if (is_null($spu))
            throw new ApiException(ApiException::ERROR_DATA_NOT_EXISTS);
        $spu->update($data);

        if (isset($data['sku'])) {
            $this->createSku($spu, $data['sku'], false);

        }

        return [];
    }

    public function getProductListV1($data)
    {
        return $this->spu->getProductListV1($data);
    }

    public function getProductInfoV1($data)
    {
        return $this->spu->getProductInfoV1($data);
    }
}
